﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using throneOfGames.Models;

namespace throneOfGames.Controllers
{
    public class GameController : Controller
    {
        private IThroneRepository repo = null;

        public GameController()
        {
            repo = new ThroneRepository();
        }
        public GameController(IThroneRepository rep)
        {
            repo = rep;
        }

        public ActionResult Index()
        {
            return View();
        }

        [Authorize]
        public ActionResult KotGame(int? id)
        {
            try
            {
                if (id.HasValue)
                {
                    var userName = User.Identity.Name;

                    GameViewModel gameView = new GameViewModel();
                    gameView.players = (from p in repo.AllPlayers
                                        where p.gameID == id
                                        select p).ToList();

                    gameView.game = (from g in repo.AllGames
                                     where g.ID == id
                                     select g).SingleOrDefault();

                    gameView.currPlayer = (from p in repo.AllPlayers
                                           where p.User.userName == userName
                                           && p.gameID == id
                                           select p).SingleOrDefault();

                    return View(gameView);
                }
            }
            catch
            {
                return View("Error");
            }

            return View();
        }

        [Authorize]
        public ActionResult TTTGame(int? id)
        {
            try
            {
                if (id.HasValue)
                {
                    var userName = User.Identity.Name;

                    var players = (from p in repo.AllPlayers
                                   join u in repo.AllUsers
                                   on p.userID equals u.ID
                                   where u.userName == userName
                                   && p.gameID == id
                                   select p).FirstOrDefault();

                    return View(players);
                }
            }
            catch
            {
                return View("Error");
            }

            return View();
        }

        [Authorize]
        public ActionResult Lobby(int? id, string user)
        {
            try
            {
                if (id.HasValue)
                {
                    LobbyViewModel lobbyVM = new LobbyViewModel();

                    lobbyVM.theGame = (from g in repo.AllGames
                                       where g.ID == id
                                       select g).SingleOrDefault();

                    lobbyVM.gamePlayers = (from p in repo.AllPlayers
                                           where p.gameID == id
                                           select p).ToList();

                    int numPlayer = lobbyVM.gamePlayers.Count;

                    var playerUser = (from u in repo.AllUsers
                                      where u.userName == user
                                      select u).SingleOrDefault();

                    foreach (var p in lobbyVM.gamePlayers)
                    {
                        if (p.User.userName == playerUser.userName)
                        {
                            return View(lobbyVM);
                        }
                    }

                    if (lobbyVM.theGame != null && playerUser != null && numPlayer < lobbyVM.theGame.maxPlayersOfGame)
                    {
                        repo.newPlayer(playerUser.ID, lobbyVM.theGame.ID, numPlayer + 1);
                        return View(lobbyVM);
                    }
                }
            }
            catch
            {
                return View("Error");
            }
            return View();
        }

        [HttpPost]
        [Authorize]
        public ActionResult CreateGame(string currUser, string name, string maxPlayers, string isPrivate)
        {
            try
            {
                int players = 0;
                bool publ = true;

                int.TryParse(maxPlayers, out players);

                if (isPrivate == "true")
                {
                    publ = false;
                }

                var boardgame = (from game in repo.AllBoardgames
                                 where game.name == name
                                 select game).FirstOrDefault();

                var user = (from u in repo.AllUsers
                            where u.userName == currUser
                            select u).SingleOrDefault();

                if (boardgame != null)
                {
                    var gameID = repo.createGame(user.ID, players, publ, boardgame.ID);
                    var obj = new { id = gameID, username = user.userName };
                    return Json(obj, JsonRequestBehavior.AllowGet);
                }
            }
            catch
            {
                return View("Error");
            }
            return View();
        }

        [Authorize]
        public ActionResult StartGame(int? id)
        {
            try
            {
                if (id.HasValue)
                {
                    var game = (from g in repo.AllGames
                                where g.ID == id
                                select g).SingleOrDefault();

                    if (game != null)
                    {
                        if (game.Boardgame.name == "TicTacToe")
                        {
                            return RedirectToAction("TTTGame", new { id = id });
                        }
                        else if (game.Boardgame.name == "King of Tokyo")
                        {
                            return RedirectToAction("KoTGame", new { id = id });
                        }
                    }
                }
            }
            catch
            {
                return View("Error");
            }
            return View();
        }

        [Authorize]
        public ActionResult ExitGame(string id, int? gameID)
        {
            try
            {
                if (id != null)
                {
                    var player = (from p in repo.AllPlayers
                                  where p.User.userName == id
                                  && p.gameID == gameID
                                  select p).SingleOrDefault();

                    if (player != null)
                    {
                        repo.removePlayer(player.ID);
                        return RedirectToAction("MainMenu", "Home");
                    }
                }
            }
            catch
            {
                return View("Error");
            }
            return View();
        }

        [Authorize]
        public ActionResult updatePlayerStatus(int? id)
        {
            try
            {
                if (id.HasValue)
                {
                    throneOfGamesDataContext db = new throneOfGamesDataContext();

                    var players = (from p in db.Players
                                   where p.gameID == id
                                   select new { p.playerHealth, p.playerEnergy, p.playerPoints, p.isInside }).ToList();

                    return Json(players, JsonRequestBehavior.AllowGet);
                }
            }
            catch
            {
                return View("Error");
            }

            return View();
        }
    }
}
